package br.com.j7.jdesk.persistence.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;

import br.com.j7.jdesk.persistence.listener.ServiceListener;

/**
 * Entidade Service
 * 
 * @author Edivando Alves
 *
 */
@Entity
@EntityListeners(value={ServiceListener.class})
@NamedQueries( {@NamedQuery(name="service", query="select s from Services s"), 
				@NamedQuery(name="serviceByEnterprise", query="from Services where enterprise = ?1"),
				@NamedQuery(name="serviceByServiceSituation", query="from Services where serviceSituation = ?1"),
				@NamedQuery(name="serviceByIdStateAndEnterprise", query="Select s from Services s INNER JOIN FETCH s.serviceSituation ss INNER JOIN ss.serviceState st WHERE st.idServiceState = ?1 AND s.enterprise = ?2 ORDER BY s.idService"),
				@NamedQuery(name="serviceByKambanFilter", query="Select s from Services s INNER JOIN FETCH s.serviceSituation ss INNER JOIN ss.serviceState st INNER JOIN s.serviceType sty WHERE st.idServiceState = ?1 AND s.enterprise = ?2 AND sty.idServiceType IN ( 1,2,3 ) ORDER BY s.idService")})
public class Services implements Serializable{

	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer idService;
	
	private String cdService;
	
	@Lob
	private String dsService;
	
	@ManyToOne
	@JoinColumn(name="idCustomer")
	private Customers customer;
	
	@ManyToOne
	@JoinColumn(name="idUser")
	private Users user;
	
	@ManyToOne
	@JoinColumn(name="idServiceType")
	private ServicesTypes serviceType;
	
	@ManyToOne
	@JoinColumn(name="idServiceSituation")
	private ServicesSituations serviceSituation;
	
	@Temporal(TemporalType.TIMESTAMP)
	private Date dtBeginAttendance;
	
	@Temporal(TemporalType.TIMESTAMP)
	private Date dtEndAttendance;
	
	@Temporal(TemporalType.TIMESTAMP)
	private Date dtBeginSolution;
	
	@Temporal(TemporalType.TIMESTAMP)
	private Date dtEndSolution;
	
	private String cdColorAttendanceBox;
	
	private String cdColorSolutionBox;
	
	private Boolean lgTimeAttendance;
	
	private Boolean lgTimeSolution;
	
	@ManyToOne
	@JoinColumn(name="idEnterprise")
	private Enterprises enterprise;
	
	@OneToMany(mappedBy="service", cascade=CascadeType.REMOVE)
	private List<ServicesLogs> servicesLogs;
	
	@Version
	private Integer nrVersion;
	
	@Transient
	private String annotation;
	
	public Services() {
		this.servicesLogs = new ArrayList<>();
		this.lgTimeAttendance = true;
		this.lgTimeSolution = true;
	}
	
	public Integer getIdService() {
		return idService;
	}

	public void setIdService(Integer idService) {
		this.idService = idService;
	}

	public String getCdService() {
		return cdService;
	}

	public void setCdService(String cdService) {
		this.cdService = cdService;
	}

	public String getDsService() {
		return dsService;
	}

	public void setDsService(String dsService) {
		this.dsService = dsService;
	}

	public Customers getCustomer() {
		if (customer == null) {
			customer = new Customers();
		}
		return customer;
	}

	public void setCustomer(Customers customer) {
		this.customer = customer;
	}

	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	public ServicesTypes getServiceType() {
		if(serviceType == null){
			serviceType = new ServicesTypes();
		}
		return serviceType;
	}

	public void setServiceType(ServicesTypes serviceType) {
		this.serviceType = serviceType;
	}
	
	public ServicesSituations getServiceSituation() {
		if (serviceSituation == null) {
			serviceSituation = new ServicesSituations();
		}
		return serviceSituation;
	}

	public void setServiceSituation(ServicesSituations serviceSituation) {
		this.serviceSituation = serviceSituation;
	}

	public Date getDtBeginAttendance() {
		return dtBeginAttendance;
	}

	public void setDtBeginAttendance(Date dtBeginAttendance) {
		this.dtBeginAttendance = dtBeginAttendance;
	}

	public Date getDtEndAttendance() {
		return dtEndAttendance;
	}

	public void setDtEndAttendance(Date dtEndAttendance) {
		this.dtEndAttendance = dtEndAttendance;
	}

	public Date getDtBeginSolution() {
		return dtBeginSolution;
	}

	public void setDtBeginSolution(Date dtBeginSolution) {
		this.dtBeginSolution = dtBeginSolution;
	}

	public Date getDtEndSolution() {
		return dtEndSolution;
	}

	public void setDtEndSolution(Date dtEndSolution) {
		this.dtEndSolution = dtEndSolution;
	}

	public String getCdColorAttendanceBox() {
		return cdColorAttendanceBox;
	}

	public void setCdColorAttendanceBox(String cdColorAttendanceBox) {
		this.cdColorAttendanceBox = cdColorAttendanceBox;
	}

	public String getCdColorSolutionBox() {
		return cdColorSolutionBox;
	}

	public void setCdColorSolutionBox(String cdColorSolutionBox) {
		this.cdColorSolutionBox = cdColorSolutionBox;
	}

	public Enterprises getEnterprise() {
		return enterprise;
	}
	
	public void setEnterprise(Enterprises enterprise) {
		this.enterprise = enterprise;
	}
	
	public Integer getNrVersion() {
		return nrVersion;
	}
	
	public void setNrVersion(Integer nrVersion) {
		this.nrVersion = nrVersion;
	}

	public Boolean getLgTimeAttendance() {
		return lgTimeAttendance;
	}

	public void setLgTimeAttendance(Boolean lgTimeAttendance) {
		this.lgTimeAttendance = lgTimeAttendance;
	}

	public Boolean getLgTimeSolution() {
		return lgTimeSolution;
	}

	public void setLgTimeSolution(Boolean lgTimeSolution) {
		this.lgTimeSolution = lgTimeSolution;
	}
	
	public List<ServicesLogs> getServicesLogs() {
		return servicesLogs;
	}
	
	public void setServicesLogs(List<ServicesLogs> servicesLogs) {
		this.servicesLogs = servicesLogs;
	}
	
	public void addServiceLog( ServicesLogs serviceLog ){
		this.servicesLogs.add( serviceLog );
	}
	
	public ServicesLogs getEndServiceLog(){
		if( servicesLogs.size() == 0 ){
			return null;
		}
		return servicesLogs.get(servicesLogs.size());
	}
	
	public String getAnnotation() {
		return annotation;
	}
	
	public void setAnnotation(String annotation) {
		this.annotation = annotation;
	}
	
	@Override
	public int hashCode() {
		return idService;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Services other = (Services) obj;
		if (idService == null) {
			if (other.idService != null)
				return false;
		} else if (!idService.equals(other.idService))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return cdService;
	}
	
}
